
	.globl _i2c			;global function _i2c
	.globl _i2c_init
	.globl _i2c_read		;global function _i2c
	.globl _i2c_write		;global function _i2c
	.globl _i2c_sendData		;global function _i2c
	.globl _i2c_readData		;global function _i2c
	.globl _i2c_stop
	
	

.area CSEG		;start of code segment


_i2c_write0:	;sending 0	
		CLR	P1.1
		NOP
		SETB	P1.0
		NOP
		NOP
		NOP
		NOP	
		NOP
		NOP
		NOP
		CLR	P1.0
		NOP
		;end of 0
		RET



_i2c_write1:	;sending 1
		SETB	P1.1
		NOP
		SETB	P1.0
		NOP
		NOP
		NOP
		NOP	
		NOP
		NOP
		NOP
		CLR	P1.0
		CLR	P1.1
		;end of 1
		RET



_i2c_start: 	SETB	P1.1		;Generate Start Condition
		NOP
		NOP
		SETB	P1.0		
		NOP
		NOP
		NOP
		CLR	P1.1
		NOP
		NOP	
		NOP
		CLR	P1.0
		NOP
		NOP
		;end of start, sending control bit
		ret

_i2c_stop:	;guarantee bus free time
		NOP
		CLR	P1.1
		NOP
		NOP
		NOP
		NOP
		NOP
		NOP
		SETB	P1.0		;Generate Stop Condition
		NOP
		NOP	
		NOP
		SETB	P1.1
		NOP
		NOP
		NOP
		CLR	P1.0
		NOP
		NOP
		;end of start, sending control bit
		RET

_i2c_ack:
	
	NOP
	;acknowledge
	SETB	P1.1
	NOP
	SETB	P1.0
	JNB	P1.1,ack
	MOV	r4,#00
	SJMP	resume
ack:	MOV	r4,#01	
	SJMP end
resume:	NOP
	NOP
end:	CLR	P1.0
	NOP
	NOP	
	NOP
	ret



	
_i2c:
	lcall _i2c_start
	
	
	
	lcall _i2c_write0


	lcall _i2c_write1

	
	lcall _i2c_write0

	
	lcall _i2c_write0
	

	;A2 A1 A0 Sequence : 0 0 0

	lcall _i2c_write0

	lcall _i2c_write0

	lcall _i2c_write0

	;READ WRITE Bit, Writing Now

	lcall _i2c_write0

	lcall _i2c_ack
	
	ret



_i2c_init:	
		push 	_bp		;push existing stack fram pointer value in stack
		mov 	_bp,sp 		;stack contains the return address, save it into stack frame pointer

		mov 	a,dpl 		;dpl contains the lower 8 bits of the leftmost variable

		mov 	r0,#03
return1:	rr	a	
		djnz	r0,return1
		
		lcall _i2c_start
	
	
		;device code here
		lcall _i2c_write1
	
		lcall _i2c_write0

		lcall _i2c_write1

		lcall _i2c_write0
	
		
		
		mov 	r0,#03
		;B2 B1 B0 Sequence :
		
		clr	c
back1:		rlc	a
		jnc	send0
		lcall _i2c_write1
		sjmp	skip_send0
send0:		lcall _i2c_write0
skip_send0:	djnz	r0,back1		
		pop 	_bp		;get return address
		ret 			;return
		






		

		





_i2c_read:	
		

		lcall _i2c_write1
		lcall _i2c_ack
		RET
	


	


_i2c_write:
		
		;READ WRITE Bit, R/W=0

		lcall _i2c_write0
		lcall _i2c_ack
		RET














_i2c_sendData:	push 	_bp		;push existing stack fram pointer value in stack
		mov 	_bp,sp 		;stack contains the return address, save it into stack frame pointer

		mov 	a,dpl 		;dpl contains the lower 8 bits of the leftmost variable

		mov 	r0,#08

		clr	c
back1W:		rlc	a
		jnc	send0W
		lcall _i2c_write1
		sjmp	skip_send0W
send0W:		lcall _i2c_write0
skip_send0W:	djnz	r0,back1W
		lcall _i2c_ack
		MOV	dpl,r4		
		pop 	_bp		;get return address
		ret 			;return
		

		

		
			

_i2c_readData:

		;write to port
	
	;sending sca-0
	
	NOP
	SETB	P1.0
	NOP
	NOP
	NOP
	NOP	
	NOP
	NOP
	NOP
	CLR	P1.0
	
	;end of 1

;sending sca-1	
	NOP
	NOP
	NOP
	NOP
	NOP
	NOP
	NOP
	SETB	P1.0
	NOP
	NOP
	NOP
	NOP	
	NOP
	NOP
	NOP
	CLR	P1.0
	
	;end of 0




;sending sca-2
	NOP
	NOP
	NOP
	NOP
	NOP
	NOP
	NOP
	SETB	P1.0
	NOP
	NOP
	NOP
	NOP	
	NOP
	NOP
	NOP
	CLR	P1.0
	
	;end of 1

;sending sca-3
	NOP
	NOP
	NOP
	NOP
	NOP
	NOP
	NOP
	SETB	P1.0
	NOP
	NOP
	NOP
	NOP	
	NOP
	NOP
	NOP
	CLR	P1.0
	
	;end of 1

;sending sca-4
	NOP
	NOP
	NOP
	NOP
	NOP
	NOP
	NOP
	SETB	P1.0
	NOP
	NOP
	NOP
	NOP	
	NOP
	NOP
	NOP
	CLR	P1.0
	
	;end of 1

;sending sca-5
	NOP
	NOP
	NOP
	NOP
	NOP
	NOP
	NOP
	NOP
	SETB	P1.0
	NOP
	NOP
	NOP
	NOP	
	NOP
	NOP
	NOP
	CLR	P1.0
	
	;end of 1

;sending sca-6
	NOP
	NOP
	NOP
	NOP
	NOP
	NOP
	NOP
	SETB	P1.0
	NOP
	NOP
	NOP
	NOP	
	NOP
	NOP
	NOP
	CLR	P1.0
	
	;end of 1

;sending sca-7
	NOP
	NOP
	NOP
	NOP
	NOP
	NOP
	NOP
	SETB	P1.0
	NOP
	NOP
	NOP
	NOP	
	NOP
	NOP
	NOP
	CLR	P1.0
	
	;end of 1


;sending sca-7
	NOP
	NOP
	NOP
	NOP
	NOP
	NOP
	NOP
	SETB	P1.0
	NOP
	NOP
	NOP
	NOP	
	NOP
	NOP
	NOP
	CLR	P1.0
	
	;end of 1





	
	;guarantee bus free time
	NOP
	CLR	P1.1
	NOP
	NOP
	NOP
	NOP
	NOP
	NOP
	SETB	P1.0		;Generate Stop Condition
	NOP
	NOP	
	NOP
	SETB	P1.1
	NOP
	NOP
	NOP
	CLR	P1.0
	NOP
	NOP
	;end of start, sending control bit
	ret











	
	
	
		
	
